// pages/repairList/repairList.js
import Toast from '@vant/weapp/toast/toast';
var globalFn = require('../../utils/globalFn.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:app.globalData.userInfo,
    serviceUrl:app.globalData.serviceUrl,
    tabList:[
      {
        text:"全部",
        status:null,
        list:[],
        data:[]
      },
      {
        text:"待审核",
        status:0,
        list:[],
        data:[]
      },
      {
        text:"驳回",
        status:1,
        list:[],
        data:[]
      },
      {
        text:"待接单",
        status:2,
        list:[],
        data:[]
      },
      {
        text:"已接单",
        status:3,
        list:[],
        data:[]
      },
      {
        text:"已完成",
        status:5,
        list:[],
        data:[]
      },
      {
        text:"已评价",
        status:6,
        list:[],
        data:[]
      }
    ],
    tabList2:[
      {
        text:"全部",
        status:null,
        list:[],
        data:[]
      },
      {
        text:"待审核",
        status:0,
        list:[],
        data:[]
      },
      {
        text:"驳回",
        status:1,
        list:[],
        data:[]
      },
      {
        text:"待接单",
        status:2,
        list:[],
        data:[]
      },
      {
        text:"已接单",
        status:3,
        list:[],
        data:[]
      },
      {
        text:"已完成",
        status:5,
        list:[],
        data:[]
      },
      {
        text:"已评价",
        status:6,
        list:[],
        data:[]
      }
    ],
    active:0,
    status:[
      {
        text:"待审核",
        type:"danger"
      },
      {
        text:"驳回",
        type:"danger"
      },
      {
        text:"待接单",
        type:"danger"
      },
      {
        text:"待处理",
        type:"danger"
      },
      {
        text:"处理中",
        type:"warning"
      },
      {
        text:"已完成",
        type:"success"
      },
      {
        text:"已评价",
        type:"primary"
      }
    ],
    repairList:[],
    delRepairId:[],
    delShow:true,
    repairDetail:false,
    feedBack:null,
    repair:null,
    steps:[
      {text:'待审核'},
      {text:'待接单'},
      {text:'待处理'},
      {text:'处理中'},
      {text:'已完成'}
    ],
    step:[
      {text:'待审核'},
      {text:'待接单'},
      {text:'待处理'},
      {text:'处理中'},
      {text:'已完成'}
    ],
    stepsActive:0,
    appraise:false,
    appraiseScore:0,
    appraiseId:0,
    appraiseContentSize:{
      minHeight: 70
    },
    feedBackList:[],
    page:1,
    limit:5
  },
  getRepairList(){
    let that = this;
    that.setData({
      tabList:that.data.tabList2,
      page:1
    })
    let tabList = that.data.tabList;
    wx.request({
      url: app.globalData.serviceUrl+'/repair/getRepairList',
      method:"POST",
      success:(res)=>{
        if(res.data.code==1){
          that.setData({
            repairList:res.data.data.filter(item=>item.userID===that.data.userInfo.id&&item.userType===app.globalData.storage.userType)
          })
          tabList.forEach(item=>{
            item.data = item.status==null?that.data.repairList:that.data.repairList.filter(it=>it.status==item.status)
            item.list = item.status==null?that.data.repairList.slice(0,that.data.page*that.data.limit):that.data.repairList.filter(it=>it.status==item.status).slice(0,that.data.page*that.data.limit);
          })
          that.setData({
            tabList:tabList
          })
          wx.stopPullDownRefresh();
        }
      },
      fail:(err)=>{
        console.log(err);
      }
    })
  },
  onChangeTabs(e){
    this.setData({
      page:1,
      active:e.detail.name
    })
  },
  checkboxChange(e){
    console.log(e);
    this.setData({
      delRepairId:e.detail.value.map(Number)
    })
  },
  delRepairById(e){
    let id = e.currentTarget.dataset.id;
    this.delRepair([id]);
  },
  delRepairByIds(){
    if(this.data.delRepairId.length<=0){
      Toast("请选择要删除的数据");
      return
    }
    this.delRepair(this.data.delRepairId);
  },
  
  showDelBtn(){
    this.setData({
      delShow:!this.data.delShow
    })
  },
  delRepair(id){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.serviceUrl+'/repair/deleteRepair',
            method:"POST",
            header:{
              "content-type":"application/x-www-form-urlencoded"
            },
            data:{
              id:id
            },
            success:(res)=>{
              if(res.data.code==1){
                wx.showToast({
                  title: '删除成功',
                  icon:"none"
                })
                that.getRepairList();
                return;
              }
              wx.showToast({
                title: '删除失败',
                icon:"none"
              })
            },
            fail:(err)=>{
              console.log(err);
            }
          })
        }
      }
    })
  },
  changeRepairDetail(e){
    this.setData({
      steps:this.data.step
    })
    let repair = this.data.repairList.filter(item=>item.id==e.currentTarget.dataset.id)[0];
    if(Object.prototype.toString.call(repair.userInfo) !== '[object Object]'){
      repair.userInfo = JSON.parse(repair.userInfo)
      var reg=/,$/gi;
      repair.repairImage=repair.repairImage.replace(reg,"").replace(/\s/g,"");
      repair.repairImage = repair.repairImage.split(',')
      if(repair.workerID!==0){
        repair.workerInfo = JSON.parse(repair.workerInfo)
      }
    }
    if(repair.status==1){
      let arr = [this.data.steps[0]];
      arr.push({text:"驳回"})
      this.setData({
        steps:arr
      })
    }
    if(repair.status==this.data.status.length-1){
      this.setData({
        feedBack:this.data.feedBackList.filter(item=>item.applicationID===e.currentTarget.dataset.id)[0]
      })
    }
    this.setData({
      repair:repair,
      stepsActive:repair.status>1?repair.status-1:repair.status,
      repairDetail:true
    })
  },
  closeRepairDetail(){
    this.setData({
      repairDetail:false
    })
  },
  previewImage(e){
    let index = e.currentTarget.dataset.index // 找到点击图片的索引位置
    wx.previewImage({
      current: this.data.serviceUrl+this.data.repair.repairImage[index], // 当前显示图片的http链接，将点击图片的索引数指定好
      urls: this.data.repair.repairImage.map(item=>item=this.data.serviceUrl+item) // 需要预览的图片http链接列表
    })
  },
  changeAppraise(e){
    this.setData({
      appraiseId:e.currentTarget.dataset.id,
      appraise:true
    })
  },
  onCloseAppraise(){
    this.setData({
      appraiseScore:0,
      appraise:false
    })
  },
  submitAppraise(e){
    let appraiseContent = e.detail.value.appraiseContent;
    let that = this;
    if(that.data.appraiseScore<=0){
      Toast("打个评分吧")
      return
    }
    if(appraiseContent==''){
      Toast("请填写您的理由")
      return
    }
    wx.request({
      url: that.data.serviceUrl+'/feedback/submitFeedback',
      method:"POST",
      data:{
        userID:app.globalData.userInfo.id,
        userType:app.globalData.storage.userType,
        applicationID:that.data.appraiseId,
        feedback:appraiseContent,
        score:that.data.appraiseScore
      },
      success:(res)=>{
        if(res.data.code==1){
          that.onCloseAppraise()
          that.getRepairList()
        }
        Toast(res.data.msg)
      }
    })
    console.log(e);
  },
  onChangeAppraiseScore(e){
    this.setData({
      appraiseScore: e.detail,
    });
  },
  getFeedBackList(){
    let that = this;
    wx.request({
      url: that.data.serviceUrl+'/feedback/getFeedbackList',
      method:"POST",
      success:(res)=>{
        if(res.data.code==1){
          that.setData({
            feedBackList:res.data.data.filter(item=>item.userID==app.globalData.userInfo.id&&item.userType==app.globalData.storage.userType)
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let toast = Toast.loading({
      mask: false,
      message: '加载中...'
    })
    globalFn.myFn.getUserInfo();
    this.setData({
      userInfo:app.globalData.userInfo,
      active:options.active*1
    })
    this.getRepairList();
    this.getFeedBackList();
    toast.clear();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh(){
    this.getRepairList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that =this;
    if(that.data.tabList[that.data.active].list.length==that.data.tabList[that.data.active].data.length){
      return
    }
    let toast = Toast.loading("加载中")
    that.setData({
      page:that.data.page+=1
    })
    that.setData({
      [`tabList[${that.data.active}].list`]:that.data.tabList[that.data.active].data.slice(0,that.data.page*that.data.limit)
    })
    toast.clear()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})